﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using VehicleManagement.Codes;
using VehicleManagement.Models;
using System.Collections;

namespace VehicleManagement.ControllersServices
{
    public class AdminServices
    {

        /// <summary>
        /// 获取所有管理员信息
        /// </summary>
        /// <returns>管理员信息列表</returns>
        public IList<AdminInfo> GetAllAdminInfo()
        {
            IList<AdminInfo> lstModel = SqlMapperManager.Instance.Get().QueryForList<AdminInfo>("SELECT_ALL_ADMININFO", null);
            return lstModel;
        }

        /// <summary>
        /// 获取管理员信息
        /// </summary>
        /// <param name="id">管理员ID</param>
        /// <returns>管理员信息对象</returns>
        public AdminInfo GetAdminInfo(int id)
        {
            IList<AdminInfo> lstModel = SqlMapperManager.Instance.Get().QueryForList<AdminInfo>("SELECT_ADMININFO_BY_ID", id);
            AdminInfo adminInfo = lstModel.Where(u => u.AdminID == id).First();
            return adminInfo;
        }

        public int EditAdminInfo(AdminInfo adminInfo)
        {
            int row = SqlMapperManager.Instance.Get().Update("UPDATE_ADMININFO", adminInfo);
            return row;
        }

        /// <summary>
        /// 公司注册一个管理员的帐号
        /// </summary>
        /// <param name="id">管理员表信息对象</param>
        /// <returns></returns>
        public object Register(AdminInfo adminInfo)
        {
            return SqlMapperManager.Instance.Get().Insert("INSERT_NEW_ADMIN", adminInfo);
        }

        /// <summary>
        /// 公司注册一个管理员的帐号
        /// </summary>
        /// <param name="id">管理员表信息对象</param>
        /// <returns></returns>
        public int AdminRegister(Hashtable ht)
        {
            object lockValue = new object();
            object keyOfNew = new object();
            lock (lockValue)
            {
                SqlMapperManager.Instance.Get().Insert("REGISTER_ADMIN", ht);
                keyOfNew = SqlMapperManager.Instance.Get().QueryForObject("SELECT_LAST_REGISTER_ADMIN", ht["companyName"].ToString());
                return Convert.ToInt32(keyOfNew);
            }
            
        }

        public int DeleteAdmin(int id)
        {
            return SqlMapperManager.Instance.Get().Update("DELETE_Admin", id);
        }
    }
}